package easy;
/*
 * 从最后开始遍历字符串，到第一个不为空格的位置开始计数
 */
public class T58 {
    public static void main(String[] args) {
        System.out.println(lengthOfLastWord("a "));
    }
    // 遍历
    public static int lengthOfLastWord(String s) {
        boolean firstSpan = true;
        int duoYuLen = 0;
        for (int i = s.toCharArray().length - 1; i >= 0; i--) {
            if (s.charAt(i) == ' ' && !firstSpan) {
                return s.length() - i - 1 - duoYuLen;
            } else {
                if (s.charAt(i) != ' ') {
                    firstSpan = false;
                } else {
                    duoYuLen++;
                }

            }
        }
        return s.length() - duoYuLen;
    }
}
